package com.stylefeng.guns.core.util.clm;

import java.io.File;

import org.apache.commons.lang.StringUtils;
import org.aspectj.util.FileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.stylefeng.guns.config.properties.GunsProperties;
import com.stylefeng.guns.core.util.SpringContextHolder;
import com.stylefeng.guns.modular.clm.controller.ClmRoomController;




/**
 * 直播上传图片
 * @author 
 *
 */
public class LiveUploadImg {
	private final static Logger logger = LoggerFactory.getLogger(ClmRoomController.class);
	
	public static synchronized String upImg(String img){
    	String realPath="";
    	try {
    		GunsProperties gunsProperties = SpringContextHolder.getBean(GunsProperties.class);
    		//在本地保存上传的图片
        	String adminHUploadUrl = gunsProperties.getFileUploadPath() + img;
    		File fromHFile=new File(adminHUploadUrl);
        	File toHFile=new File(gunsProperties.getFileAdminUploadPath()+img);
        	FileUtil.copyFile(fromHFile, toHFile);
        	
        	//掉接口上传图片
        	String uploadUrl = "_tmp/" + img;
    		String responJson=AdminTools.updateImageUrl("admin", uploadUrl,AdminConstant.DEFAULT_SITEID);
    		logger.info("update img="+responJson);
    		if(StringUtils.isNotBlank(responJson)){
    			JSONObject jsonObj=JSON.parseObject(responJson);
    			if(!"0".equals(jsonObj.getString("result"))){
    				realPath=jsonObj.getString("realPath");
    				
    			}
    			
    		}
		} catch (Exception e) {
			e.printStackTrace();
			return realPath;
		}
    	
    	return realPath;
    }
	
}